package president;

import home.HomePage;

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKCashAdvance;
import controllers.BKLiquidation;
import controllers.BKPayroll;
import controllers.BKSubsidiary;

public class Payroll extends WebPage {
	
	

		public Payroll() throws SQLException, IOException
		{
			
			Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
			SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
			
			add(new BookmarkablePageLink("home", PresidentHome.class));
			add(new BookmarkablePageLink("logoutPage", HomePage.class));
			add(new BookmarkablePageLink("cash-advance", Forms.class));
			add(new BookmarkablePageLink("liquidation", Liquidation.class));
			add(new BookmarkablePageLink("subsidiary", Subsidiary.class));
			add(new BookmarkablePageLink("payroll", Payroll.class));
			add(new BookmarkablePageLink("properties", Properties.class));
			
			Form form = new Form("PendingFormsListForm");
			
			
			
			form.add(new Button("view"){
				private static final long serialVersionUID = 1L;
				
				public void onSubmit(){
					try{
						if(selections.size()!=1){
							error("Please choose one account.");
						}
						else{
							int formID = selections.get(0);
												
			    			ViewPayroll page = new ViewPayroll(formID);
			    			setResponsePage(page);
						}
		    		}catch (IOException e){
		    			e.printStackTrace();
		    		}catch (SQLException e){
		    			e.printStackTrace();
		    		}
				}
			});
			
			
			List<BKPayroll> list = (List<BKPayroll>)sqlMap.queryForList("BKPayroll.getAllPending", "PENDING");
			
			ListView bkp = new ListView("bkp", list) {
				private static final long serialVersionUID = 1L;
				
				protected void populateItem(ListItem item) {
					final BKPayroll bkp = (BKPayroll) item.getModelObject();
					
					item.add(new CustomCheckBox("checkbox", new CustomCheckBoxModel(bkp.getBkp_id())));
					item.add(new Label("id", Integer.toString(bkp.getBkp_id())));
					item.add(new Label("date", bkp.getDate()));
					item.add(new Label("activity", bkp.getActivity()));
					item.add(new Label("purpose", bkp.getPurpose()));
					item.add(new Label("disbursedBy", bkp.getDisbursedBy()));
					item.add(new Label("approvedBy", bkp.getApprovedBy()));
					
					}
			};
			
			form.add(bkp);
			add(form);
			
		}
		
		public class CustomCheckBox extends CheckBox{
			private static final long serialVersionUID = 1L;

			public CustomCheckBox(String id, IModel model) {
				super(id, model);
			}
			
			protected boolean wantOnSelectionChangedNotifications() {
				return true;
			}
		}

		private final List<Integer> selections = new ArrayList();
		
		private class CustomCheckBoxModel implements IModel, Serializable{
			private static final long serialVersionUID = 1L;
			private final int id;
			
			public CustomCheckBoxModel(int id) {
				super();
				this.id = id;
			}

			public IModel getNestedModel() {
				return null;
			}

			public Object getObject(){
				return isSelected();
			}
			
			private Boolean isSelected() {
				if (selections.contains(id))
					return true;
				else
					return false;
			}
				
			public void setObject(Object object){
				boolean selected = ((Boolean) object).booleanValue();
				boolean previouslySelected = isSelected();
				if (selected){
					if (!previouslySelected) {
						selections.add(id);
					}
				} else {
					if (previouslySelected) {
						selections.remove((Integer)id);
					}
				}
			}
			public void detach() {}
		}
}
